<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="用户编号" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="真实姓名" prop="realName">
        <el-input
          v-model="queryParams.realName"
          placeholder="请输入真实姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="idNumber">
        <el-input
          v-model="queryParams.idNumber"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-input
          v-model="queryParams.gender"
          placeholder="请输入性别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="籍贯" prop="nativePlace">
        <el-input
          v-model="queryParams.nativePlace"
          placeholder="请输入籍贯"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出生日期">
        <el-date-picker
          v-model="daterangeDateOfBirth"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="身份证有效日期">
        <el-date-picker
          v-model="daterangeValidityPeriodIdentityCard"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="家庭住址" prop="familyAddress">
        <el-input
          v-model="queryParams.familyAddress"
          placeholder="请输入家庭住址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="初次领证日期">
        <el-date-picker
          v-model="daterangeDateFirstApplication"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="驾驶证有效日期">
        <el-date-picker
          v-model="daterangeValidityPeriodDrivingLicense"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="信用分" prop="creditScore">
        <el-input
          v-model="queryParams.creditScore"
          placeholder="请输入信用分"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="cellPhoneNumber">
        <el-input
          v-model="queryParams.cellPhoneNumber"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电子邮箱" prop="emailAddress">
        <el-input
          v-model="queryParams.emailAddress"
          placeholder="请输入电子邮箱"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['danny:danny:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['danny:danny:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['danny:danny:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['danny:danny:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="dannyList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="司机编号" align="center" prop="driverId" />
      <el-table-column label="用户编号" align="center" prop="userId" />
      <el-table-column label="真实姓名" align="center" prop="realName" />
      <el-table-column label="身份证号" align="center" prop="idNumber" />
      <el-table-column label="性别" align="center" prop="gender" />
      <el-table-column label="籍贯" align="center" prop="nativePlace" />
      <el-table-column
        label="出生日期"
        align="center"
        prop="dateOfBirth"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dateOfBirth, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="身份证有效日期"
        align="center"
        prop="validityPeriodIdentityCard"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.validityPeriodIdentityCard, "{y}-{m}-{d}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="家庭住址" align="center" prop="familyAddress" />
      <el-table-column
        label="身份证正面照"
        align="center"
        prop="frontPhotoCard"
        width="100"
      >
        <template slot-scope="scope">
          <image-preview
            :src="scope.row.frontPhotoCard"
            :width="50"
            :height="50"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="身份证反面照"
        align="center"
        prop="reversePhotoCard"
        width="100"
      >
        <template slot-scope="scope">
          <image-preview
            :src="scope.row.reversePhotoCard"
            :width="50"
            :height="50"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="初次领证日期"
        align="center"
        prop="dateFirstApplication"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.dateFirstApplication, "{y}-{m}-{d}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="驾驶证有效日期"
        align="center"
        prop="validityPeriodDrivingLicense"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.validityPeriodDrivingLicense, "{y}-{m}-{d}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="准驾车型"
        align="center"
        prop="quasiDrivingType"
      />
      <el-table-column
        label="驾驶证正本照"
        align="center"
        prop="drivingLicenceFrontPicture"
        width="100"
      >
        <template slot-scope="scope">
          <image-preview
            :src="scope.row.drivingLicenceFrontPicture"
            :width="50"
            :height="50"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="驾驶证副本照"
        align="center"
        prop="drivingLicenceContraryPicture"
        width="100"
      >
        <template slot-scope="scope">
          <image-preview
            :src="scope.row.drivingLicenceContraryPicture"
            :width="50"
            :height="50"
          />
        </template>
      </el-table-column>
      <el-table-column label="信用分" align="center" prop="creditScore" />
      <el-table-column label="审批状态" align="center" prop="approveStatus" />
      <el-table-column label="手机号" align="center" prop="cellPhoneNumber" />
      <el-table-column label="电子邮箱" align="center" prop="emailAddress" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['danny:danny:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['danny:danny:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改司机对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户编号" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户编号" />
        </el-form-item>
        <el-form-item label="真实姓名" prop="realName">
          <el-input v-model="form.realName" placeholder="请输入真实姓名" />
        </el-form-item>
        <el-form-item label="身份证号" prop="idNumber">
          <el-input v-model="form.idNumber" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-input v-model="form.gender" placeholder="请输入性别" />
        </el-form-item>
        <el-form-item label="籍贯" prop="nativePlace">
          <el-input v-model="form.nativePlace" placeholder="请输入籍贯" />
        </el-form-item>
        <el-form-item label="出生日期" prop="dateOfBirth">
          <el-date-picker
            clearable
            v-model="form.dateOfBirth"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择出生日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="身份证有效日期" prop="validityPeriodIdentityCard">
          <el-date-picker
            clearable
            v-model="form.validityPeriodIdentityCard"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择身份证有效日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="家庭住址" prop="familyAddress">
          <el-input v-model="form.familyAddress" placeholder="请输入家庭住址" />
        </el-form-item>
        <el-form-item label="身份证正面照">
          <image-upload v-model="form.frontPhotoCard" />
        </el-form-item>
        <el-form-item label="身份证反面照">
          <image-upload v-model="form.reversePhotoCard" />
        </el-form-item>
        <el-form-item label="初次领证日期" prop="dateFirstApplication">
          <el-date-picker
            clearable
            v-model="form.dateFirstApplication"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择初次领证日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item
          label="驾驶证有效日期"
          prop="validityPeriodDrivingLicense"
        >
          <el-date-picker
            clearable
            v-model="form.validityPeriodDrivingLicense"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择驾驶证有效日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="驾驶证正本照">
          <image-upload v-model="form.drivingLicenceFrontPicture" />
        </el-form-item>
        <el-form-item label="驾驶证副本照">
          <image-upload v-model="form.drivingLicenceContraryPicture" />
        </el-form-item>
        <el-form-item label="信用分" prop="creditScore">
          <el-input v-model="form.creditScore" placeholder="请输入信用分" />
        </el-form-item>
        <el-form-item label="手机号" prop="cellPhoneNumber">
          <el-input v-model="form.cellPhoneNumber" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="电子邮箱" prop="emailAddress">
          <el-input v-model="form.emailAddress" placeholder="请输入电子邮箱" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listDanny,
  getDanny,
  delDanny,
  addDanny,
  updateDanny,
} from "@/api/danny/danny";

export default {
  name: "Danny",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 司机表格数据
      dannyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 电子邮箱时间范围
      daterangeDateOfBirth: [],
      // 电子邮箱时间范围
      daterangeValidityPeriodIdentityCard: [],
      // 电子邮箱时间范围
      daterangeDateFirstApplication: [],
      // 电子邮箱时间范围
      daterangeValidityPeriodDrivingLicense: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        realName: null,
        idNumber: null,
        gender: null,
        nativePlace: null,
        dateOfBirth: null,
        validityPeriodIdentityCard: null,
        familyAddress: null,
        frontPhotoCard: null,
        reversePhotoCard: null,
        dateFirstApplication: null,
        validityPeriodDrivingLicense: null,
        quasiDrivingType: null,
        drivingLicenceFrontPicture: null,
        drivingLicenceContraryPicture: null,
        creditScore: null,
        approveStatus: null,
        cellPhoneNumber: null,
        emailAddress: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        realName: [
          { required: true, message: "真实姓名不能为空", trigger: "blur" },
        ],
        idNumber: [
          { required: true, message: "身份证号不能为空", trigger: "blur" },
        ],
        gender: [{ required: true, message: "性别不能为空", trigger: "blur" }],
        nativePlace: [
          { required: true, message: "籍贯不能为空", trigger: "blur" },
        ],
        dateOfBirth: [
          { required: true, message: "出生日期不能为空", trigger: "blur" },
        ],
        validityPeriodIdentityCard: [
          {
            required: true,
            message: "身份证有效日期不能为空",
            trigger: "blur",
          },
        ],
        familyAddress: [
          { required: true, message: "家庭住址不能为空", trigger: "blur" },
        ],
        frontPhotoCard: [
          { required: true, message: "身份证正面照不能为空", trigger: "blur" },
        ],
        reversePhotoCard: [
          { required: true, message: "身份证反面照不能为空", trigger: "blur" },
        ],
        dateFirstApplication: [
          { required: true, message: "初次领证日期不能为空", trigger: "blur" },
        ],
        validityPeriodDrivingLicense: [
          {
            required: true,
            message: "驾驶证有效日期不能为空",
            trigger: "blur",
          },
        ],
        quasiDrivingType: [
          { required: true, message: "准驾车型不能为空", trigger: "change" },
        ],
        drivingLicenceFrontPicture: [
          { required: true, message: "驾驶证正本照不能为空", trigger: "blur" },
        ],
        drivingLicenceContraryPicture: [
          { required: true, message: "驾驶证副本照不能为空", trigger: "blur" },
        ],
        cellPhoneNumber: [
          { required: true, message: "手机号不能为空", trigger: "blur" },
        ],
        emailAddress: [
          { required: true, message: "电子邮箱不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询司机列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (
        null != this.daterangeDateOfBirth &&
        "" != this.daterangeDateOfBirth
      ) {
        this.queryParams.params["beginDateOfBirth"] =
          this.daterangeDateOfBirth[0];
        this.queryParams.params["endDateOfBirth"] =
          this.daterangeDateOfBirth[1];
      }
      if (
        null != this.daterangeValidityPeriodIdentityCard &&
        "" != this.daterangeValidityPeriodIdentityCard
      ) {
        this.queryParams.params["beginValidityPeriodIdentityCard"] =
          this.daterangeValidityPeriodIdentityCard[0];
        this.queryParams.params["endValidityPeriodIdentityCard"] =
          this.daterangeValidityPeriodIdentityCard[1];
      }
      if (
        null != this.daterangeDateFirstApplication &&
        "" != this.daterangeDateFirstApplication
      ) {
        this.queryParams.params["beginDateFirstApplication"] =
          this.daterangeDateFirstApplication[0];
        this.queryParams.params["endDateFirstApplication"] =
          this.daterangeDateFirstApplication[1];
      }
      if (
        null != this.daterangeValidityPeriodDrivingLicense &&
        "" != this.daterangeValidityPeriodDrivingLicense
      ) {
        this.queryParams.params["beginValidityPeriodDrivingLicense"] =
          this.daterangeValidityPeriodDrivingLicense[0];
        this.queryParams.params["endValidityPeriodDrivingLicense"] =
          this.daterangeValidityPeriodDrivingLicense[1];
      }
      listDanny(this.queryParams).then((response) => {
        this.dannyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        driverId: null,
        userId: null,
        realName: null,
        idNumber: null,
        gender: null,
        nativePlace: null,
        dateOfBirth: null,
        validityPeriodIdentityCard: null,
        familyAddress: null,
        frontPhotoCard: null,
        reversePhotoCard: null,
        dateFirstApplication: null,
        validityPeriodDrivingLicense: null,
        quasiDrivingType: null,
        drivingLicenceFrontPicture: null,
        drivingLicenceContraryPicture: null,
        creditScore: null,
        approveStatus: 0,
        cellPhoneNumber: null,
        emailAddress: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeDateOfBirth = [];
      this.daterangeValidityPeriodIdentityCard = [];
      this.daterangeDateFirstApplication = [];
      this.daterangeValidityPeriodDrivingLicense = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.driverId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加司机";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const driverId = row.driverId || this.ids;
      getDanny(driverId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改司机";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.driverId != null) {
            updateDanny(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDanny(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const driverIds = row.driverId || this.ids;
      this.$modal
        .confirm('是否确认删除司机编号为"' + driverIds + '"的数据项？')
        .then(function () {
          return delDanny(driverIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "danny/danny/export",
        {
          ...this.queryParams,
        },
        `danny_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
